package com.clkj.goldpartner.service.impl;

import com.clkj.goldpartner.dao.*;
import com.clkj.goldpartner.pojo.*;
import com.clkj.goldpartner.service.StockService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author 杨行
 * @date 2021/3/2 15:33
 */
@Service
public class StockServiceImpl implements StockService {
    @Autowired
    private OutStockMapper outStockMapper;

    @Autowired
    private StockInfoMapper stockInfoMapper;

    @Autowired
    private InStockMapper inStockMapper;

    @Autowired
    private CheckStockMapper checkStockMapper;

    @Autowired
    private QualityStockMapper qualityStockMapper;

    @Autowired
    private ReviewStockMapper reviewStockMapper;

    @Autowired
    private GoodsStockMapper goodsStockMapper;

    @Autowired
    private WareChangeMapper wareChangeMapper;

    /**
     * 添加出库单
     *
     * @param outStock
     */
    @Override
    public void addOutStock(OutStock outStock) {
        outStockMapper.insertSelective(outStock);
    }

    /**
     * 修改出库单
     *
     * @param outStock
     */
    @Override
    public void editOutStock(OutStock outStock) {
        outStockMapper.updateByPrimaryKeySelective(outStock);
    }

    /**
     * 添加订单详情
     *
     * @param stockInfo
     */
    @Override
    public void addStockInfo(StockInfo stockInfo) {
        stockInfoMapper.insertSelective(stockInfo);
    }

    /**
     * 获取最后一位编号
     *
     * @return String
     */
    @Override
    public String getOut_num() {
        return outStockMapper.getOut_num();
    }

    /**
     * 获取不同状态的出货单
     *
     * @param shop_id
     * @param out_status
     * @return List<OutStock>
     */
    @Override
    public List<OutStock> getOutStock(Integer shop_id, String out_status) {
        return outStockMapper.getOutStock(shop_id, out_status);
    }

    /**
     * 获取指定的库存记录
     *
     * @param stock_status
     * @param stock_num
     * @return
     */
    @Override
    public List<Map> getStockInfo(String stock_status, String stock_num) {
        return stockInfoMapper.getStockInfo(stock_status, stock_num);
    }

    /**
     * 添加进货单
     *
     * @param inStock
     */
    @Override
    public void addInStock(InStock inStock) {
        inStockMapper.insertSelective(inStock);
    }

    /**
     * 添加确认单
     *
     * @param checkStock
     */
    @Override
    public void addCheckStock(CheckStock checkStock) {
        checkStockMapper.insertSelective(checkStock);
    }

    /**
     * 添加质检单
     *
     * @param qualityStock
     */
    @Override
    public void addQualityStock(QualityStock qualityStock) {
        qualityStockMapper.insertSelective(qualityStock);
    }

    /**
     * 添加复核单
     *
     * @param reviewStock
     */
    @Override
    public void addReviewStock(ReviewStock reviewStock) {
        reviewStockMapper.insertSelective(reviewStock);
    }

    /**
     * 获取未完成订单
     *
     * @param shop_id
     * @param supplier_id
     * @param status
     * @return
     */
    @Override
    public List<Map> getInStock(Integer shop_id, Integer supplier_id, String status) {
        return inStockMapper.getInStock(shop_id, supplier_id, status);
    }


    /**
     * 更新进货订单状态
     *
     * @param in_num
     * @param in_status
     */
    @Override
    public void updateInStock(String in_num, String in_status) {
        inStockMapper.updateInStock(in_num, in_status);
    }

    /**
     * 获取最后一位编号
     *
     * @return
     */
    @Override
    public String getIn_num() {
        return inStockMapper.getIn_num();
    }

    /**
     * 获取仓库列表
     *
     * @param shop_id
     * @param warehouse_id
     * @param in_price
     * @param category_id
     * @return
     */
    @Override
    public List<Map> getGoodsStock(Integer shop_id, Integer warehouse_id, Double in_price, Integer category_id) {
        return goodsStockMapper.getGoodsStock(shop_id, warehouse_id, in_price, category_id);
    }

    /**
     * 添加/修改商品库存
     *
     * @param shop_id
     * @param goods_id
     * @param goods_number
     * @param note
     * @param batch_number
     * @param production_date
     * @param expiration
     * @param warehouse_id
     * @param in_price
     */
    @Override
    public void addGoodsStock(Integer shop_id, Integer goods_id, Integer goods_number, String note, String batch_number, String production_date, String expiration, Integer warehouse_id, Double in_price) {
        goodsStockMapper.addGoodsStock(shop_id, goods_id, goods_number, note, batch_number, production_date, expiration, warehouse_id, in_price);
    }

    /**
     * 通过id获取库存列表
     *
     * @param ids
     * @return List<GoodsStock>
     */
    @Override
    public List<GoodsStock> getGoodsStockById(String ids) {
        return goodsStockMapper.getGoodsStockById(ids);
    }

    /**
     * 修改库存
     *
     * @param goodsStock
     */
    @Override
    public void editGoodsStock(GoodsStock goodsStock) {
        goodsStockMapper.updateByPrimaryKeySelective(goodsStock);
    }

    /**
     * 添加调拨
     *
     * @param wareChange
     */
    @Override
    public void addWareChange(WareChange wareChange) {
        wareChangeMapper.insertSelective(wareChange);
    }


    /**
     * 货品进货明细
     *
     * @param shop_id
     * @param goods_id
     * @param start_time
     * @param end_time
     * @return
     */
    @Override
    public List<Map<String, Object>> listGoodsInDetail(Integer shop_id, Integer goods_id, String start_time, String end_time) {
        return inStockMapper.listGoodsInDetail(shop_id, goods_id, start_time+" 00:00:00", end_time+" 23:59:59");
    }

    /**
     * 供应商供货明细
     *
     * @param shop_id
     * @param supplier
     * @param start_time
     * @param end_time
     * @return
     */
    @Override
    public List<Map<String, Object>> listGyDetail(Integer shop_id, Integer supplier, String start_time, String end_time) {
        return inStockMapper.listGyDetail(shop_id, supplier, start_time, end_time);
    }

    /**
     * 货品销售明细
     *
     * @param shop_id
     * @param goods_id
     * @param start_time
     * @param end_time
     * @param flag       flag--0货品销售明细,flag--1业务员销售明细，flag--2客户销售明细
     * @return
     */
    @Override
    public List<Map<String, Object>> listGoodsSaleDetail(Integer shop_id, Integer goods_id, String start_time, String end_time, String flag) {
        return inStockMapper.listGoodsSaleDetail(shop_id, goods_id, start_time, end_time, flag);
    }

    /**
     * 商品库存详情
     *
     * @param shop_id
     * @param goods_id
     * @param warehouse_id
     * @param goods_num
     * @param category_id
     * @param flag
     * @return
     */
    @Override
    public List<Map<String, Object>> listStockDetail(Integer shop_id, Integer goods_id, Integer warehouse_id, String goods_num, Integer category_id, Integer flag) {
        return inStockMapper.listStockDetail(shop_id, goods_id, warehouse_id, goods_num, category_id, flag);
    }

    /**
     * 库存数量汇总
     *
     * @param shop_id
     * @param warehouse_id
     * @return
     */
    @Override
    public List<Map<String, Object>> stockTotalList(Integer shop_id, Integer warehouse_id) {
        return inStockMapper.stockTotalList(shop_id, warehouse_id);
    }

    /**
     * 库存不足预警
     *
     * @param shop_id
     * @param warehouse_id
     * @return
     */
    @Override
    public List<Map<String, Object>> listStockWarning(Integer shop_id, Integer warehouse_id) {
        return inStockMapper.listStockWarning(shop_id, warehouse_id);
    }

    /**
     * 更新入库仓库id
     *
     * @param stock_num
     * @param warehouse_id
     */
    @Override
    public void updateStockInfoWarehouse(String stock_num, Integer warehouse_id) {
        inStockMapper.updateStockInfoWarehouse(warehouse_id, stock_num);
    }

    /**
     * 获取到期商品库存
     *
     * @param shop_id
     * @return
     */
    @Override
    public List<Map> getGoodsStockWarn(Integer shop_id) {
        return goodsStockMapper.getGoodsStockWarn(shop_id);
    }
}
